package com.example.yueliang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yueliang.common.Result;
import com.example.yueliang.pojo.HotelReservation;
import com.example.yueliang.pojo.Orders;
import com.example.yueliang.pojo.Tickets;
import com.example.yueliang.service.impl.OrdersServiceImpl;
import com.example.yueliang.service.impl.TicketsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/ticket")
public class TrainController {

    @Autowired
    private TicketsServiceImpl ticketsService;

    @Autowired
    private OrdersServiceImpl ordersService;

    /**
     * 查询火车票
     * @param ticket
     * @return
     */
    @PostMapping("/search")
    public Result queryTicket(@RequestBody Tickets ticket){
        log.info(ticket.toString());

        LambdaQueryWrapper<Tickets> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Tickets::getDepartureStation,ticket.getDepartureStation());
        queryWrapper.like(Tickets::getArrivalStation,ticket.getArrivalStation());
        queryWrapper.like(Tickets::getDepartureTime,ticket.getDepartureTime());

        List<Tickets> ticketsList = ticketsService.list(queryWrapper);
        if (ticketsList.size() != 0){
            return Result.success(ticketsList);
        }

        return Result.success();
    }

    /**
     * 预订车票
     * @param request
     * @param ticketId
     * @return
     */
    @GetMapping("/reserve/{ticketId}")
    public Result reserveTicket(HttpServletRequest request, @PathVariable("ticketId") Integer ticketId){
        log.info(ticketId.toString());

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getTicketId,ticketId);
        queryWrapper.eq(Orders::getUserId,(int)request.getSession().getAttribute("user"));
        int count = ordersService.count(queryWrapper);

        if (count == 0){
            Orders order = new Orders();
            order.setTicketId(ticketId);
            order.setUserId((int)request.getSession().getAttribute("user"));
            order.setOrderTime(LocalDateTime.now());
            ordersService.save(order);
            return Result.success("预订成功");
        }

        return Result.error("用户已预订该车次，不可重复预订");
    }

    /**
     * 查询所有的火车票订单
     * @return
     */
    @PostMapping("/orders")
    public Result QueryTrainOrders(HttpServletRequest request){
        int userId = (int)request.getSession().getAttribute("user");

        //根据用户id 查询所有的车票订单
        LambdaQueryWrapper<Orders> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Orders::getUserId,userId);
        List<Orders> ordersList = ordersService.list(queryWrapper1);

        List<Tickets> ticketsList = new ArrayList<>();
        for (Orders order : ordersList){
            Integer ticketId = order.getTicketId();

            LambdaQueryWrapper<Tickets> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Tickets::getTicketId,ticketId);
            Tickets ticket = ticketsService.getOne(queryWrapper2);

            ticketsList.add(ticket);
        }

        return Result.success(ticketsList);
    }

    /**
     * 删除车票订单
     */
    @GetMapping("/remove")
    public Result removeHotelOrder(HttpServletRequest request,@RequestParam String trainOrderId){
        int userId = (int) request.getSession().getAttribute("user");

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getTicketId,trainOrderId);
        queryWrapper.eq(Orders::getUserId,userId);

        ordersService.remove(queryWrapper);

        return Result.success("删除成功");
    }
}
